package demo1;

import java.util.Arrays;

public class Test {
    public static void main(String[] args) {
        Student student1 = new Student("afei", 10);
        Student student2 = new Student("lisi", 6);
        Student student3 = new Student("zhangsan", 8);
        Student[] students = new Student[3];//学生数组
        students[0] = student1;
        students[1] = student2;
        students[2] = student3;
        System.out.println(Arrays.toString(students));

        AgeComparator ageComparator = new AgeComparator();
        Arrays.sort(students, ageComparator);

        System.out.println(Arrays.toString(students));
    }


    public static void main3(String[] args) {
        Student student1 = new Student("afei", 10);
        Student student2 = new Student("lisi", 6);

        System.out.println("根据年龄比较：");
        AgeComparator ageComparator = new AgeComparator();
        int ret = ageComparator.compare(student1, student2);
        if (ret > 0) {
            System.out.println("student1 > student2");
        } else if (ret == 0) {
            System.out.println("student1 == student2");
        } else {
            System.out.println("student1 < student2");
        }

        System.out.println("根据姓名比较：");
        NameComparator nameComparator = new NameComparator();
        ret = nameComparator.compare(student1, student2);
        if (ret > 0) {
            System.out.println("student1 > student2");
        } else if (ret == 0) {
            System.out.println("student1 == student2");
        } else {
            System.out.println("student1 < student2");
        }
    }

    public static void bubbleSort(Comparable[] comparables) {
        for (int i = 0; i < comparables.length; i++) {
            for (int j = 0; j < comparables.length - 1 - i; j++) {
                if (comparables[j].compareTo(comparables[j + 1]) > 0) {
                    Comparable tmp = comparables[j];
                    comparables[j] = comparables[j + 1];
                    comparables[j + 1] = tmp;
                }
            }
        }
    }

    public static void main2(String[] args) {//排列学生数组
        Student student1 = new Student("afei", 10);
        Student student2 = new Student("lisi", 6);
        Student student3 = new Student("zhangsan", 8);
        Student[] students = new Student[3];//学生数组
        students[0] = student1;
        students[1] = student2;
        students[2] = student3;
        System.out.println(Arrays.toString(students));

        //Arrays.sort(students);
        bubbleSort(students);

        System.out.println(Arrays.toString(students));
    }





    public static void main1(String[] args) {//比较学生对象
//        Student student1 = new Student("afei", 10);
//        Student student2 = new Student("lisi", 6);
//
//        int ret = student1.compareTo(student2);
//        if (ret > 0) {
//            System.out.println("student1 > student2");
//        } else if (ret == 0) {
//            System.out.println("student1 == student2");
//        } else {
//            System.out.println("student1 < student2");
//        }
    }
}
